import ChangWanAdQQ from "../../AppMatrixAd/src/ChannelImp/ChangWanAdQQ";

let RankLevel = require('RankLevel');
let GameModeType = require('GameModeType');
let SAVE_DATA_KEY = require('SaveDataKey');

//玩家数据
let PlayerData = cc.Class({
  extends: cc.Component,
  statics: {
    /**
     *  {PlayerData}
     */
    instance: null,
  },
  
  ctor() {
    //base user info只存能够从微信拿到的
    this.userInfo = {
      avatarUrl: "",
      nickName: "",
      province: "",
      city: "",
      gender: "",
      createdAt: undefined,             //用户创建时间, 时间戳
      yesterdayInvitedCount: 0,         //用户昨天邀请到新用户数量
    };
    
    this.gameData = GameConfig.defaultPlayerData;
    this.userId = undefined;
    
    this.shareAward = {};
  },
  

  setNetData(netData) {
    if (netData.userInfo !== undefined) {
      this.applyUserInfo(netData.userInfo);
    }
    if (netData.gameData !== undefined) {
      this.gameData = netData.gameData;
    }
    if (netData.shareAward !== undefined) {
      this.shareAward = netData.shareAward;
    }
    if (netData.createTime !== undefined) {
      this.userInfo.createdAt = netData.createTime;
    }
    cc.CEventManager.instance.dispatchEvent(EVENTS.SET_NET_DATA);
  },
  
  /**
   * 保存玩家数据
   */
  saveData(isExit = false) {
    LocalData.setItem(SAVE_DATA_KEY, this.gameData);
  },
  
  /**
   * 设置用户信息
   * @param userInfo
   */
  applyUserInfo(userInfo) {
    if (!userInfo) {
      this.setUserId('no_user_id');
      return;
    }
    for (let key in userInfo) {
      if (userInfo.hasOwnProperty(key)) {
        this.userInfo[key] = userInfo[key];
      }
    }
    if (this.userInfo.id !== undefined) {
      this.setUserId(this.userInfo.id);
    }
    cc.CEventManager.instance.dispatchEvent(EVENTS.USER_INFO_UPDATED);
    
  },
  
  /**
   * 取用户信息
   * @returns {{avatarUrl: string, nickName: string, province: string, city: string, gender: string}|*}
   */
  getUserInfo() {
    return this.userInfo
  },
  
  
  /**
   * 设置用户uid
   * @param userId
   */
  setUserId(userId) {
    this.userId = userId;
  },
  
  /**
   * 取用户uid
   * @returns {*}
   */
  getUserId() {
    return this.userId;
  },
  
  /**
   * 保存退出游戏时间
   */
  saveExitTime() {
    LocalData.setItem('exitTime', new Date().getTime());
    
  },
  
  /**
   * 取退出游戏时间
   */
  getExitTime() {
    return LocalData.getItem('exitTime');
  },
  
  /**
   * 取下一个周一凌晨0点的时间戳
   */
  getNextMonday() {
    let d = new Date();
    d.setDate(d.getDate() + (1 + 7 - d.getDay()) % 7);
    d.setHours(0, 0, 0, 0);
    return d.getTime();
  },
  
  /**
   * 取历史最高分
   * @returns {number|*}
   */
  getHighestScore() {
    let mondayTime = this.getNextMonday();
    if (!this.gameData.highestScoreExpire) {
      this.gameData.highestScoreExpire = mondayTime;
    }
    if (new Date().getTime() > mondayTime) {
      this.gameData.highestScore = 0;
      this.gameData.highestScoreExpire = mondayTime;
    }
    return this.gameData.highestScore;
  },
  
  getShareAward() {
    return this.shareAward;
  },
  
  /**
   * 取等级
   * @returns {number}
   */
  getLevel() {
    return this.gameData.level;
  },
  /**
   * 报告关卡
   * @param lv
   */
  reportLevel(lv) {
    PlatformUtils.reportScores({
      reportData: {
        score: lv
      }
    });
    ChangWanAdQQ.reportScores(lv, () => {
    });
  },
  
  /**
   * 设置等级
   * @param lv
   */
  setLevel(lv) {
    let oldLv = this.gameData.level;
    let oldDef = RankLevel.getRankLevelDef(oldLv);
    this.gameData.level = lv;
    let newDef = RankLevel.getRankLevelDef(this.gameData.level);
    this.reportLevel(lv);
    this.saveData();
    cc.CEventManager.instance.dispatchEvent(EVENTS.LEVEL_CHANGED, this.gameData.level);
    if (oldDef !== newDef) {
      cc.CEventManager.instance.dispatchEvent(EVENTS.RANK_LEVEL_UP);
    }
  },
  
  /**
   * 等级加1
   */
  incLevel() {
    let oldLv = this.gameData.level;
    let oldDef = RankLevel.getRankLevelDef(oldLv);
    this.gameData.level++;
    let newDef = RankLevel.getRankLevelDef(this.gameData.level);
    this.reportLevel(this.gameData.level);
    this.saveData();
    cc.CEventManager.instance.dispatchEvent(EVENTS.LEVEL_CHANGED, this.gameData.level);
    if (oldDef !== newDef) {
      cc.CEventManager.instance.dispatchEvent(EVENTS.RANK_LEVEL_UP);
    }
  },
  
  getMoney() {
    return this.gameData.money;
  },
  
  addMoney(addMoney, worldPos) {
    this.gameData.money += addMoney;
    this.saveData();
    cc.CEventManager.instance.dispatchEvent(EVENTS.PLAYER_MONEY_CHANGE, {
      money: this.gameData.money,
      worldPos
    });
  },
  
  setMoney(money, worldPos) {
    this.gameData.money = money;
    this.saveData();
    cc.CEventManager.instance.dispatchEvent(EVENTS.PLAYER_MONEY_CHANGE, {
      money: this.gameData.money,
      worldPos
    });
  },
  
  getDiamond() {
    return this.gameData.diamond;
  },
  
  addDiamond(addDiamond, worldPos) {
    this.gameData.diamond += addDiamond;
    this.saveData();
    cc.CEventManager.instance.dispatchEvent(EVENTS.PLAYER_DIAMOND_CHANGE, {
      diamond: this.gameData.diamond,
      worldPos
    });
  },
  
  setDiamond(diamond, worldPos) {
    this.gameData.diamond = diamond;
    this.saveData();
    cc.CEventManager.instance.dispatchEvent(EVENTS.PLAYER_DIAMOND_CHANGE, {
      diamond: this.gameData.diamond,
      worldPos
    });
  },
  
  
  /**
   * 指定的skinId是否已经被锁定
   * @param skinId
   */
  isSkinLocked(skinId) {
    return this.gameData.unlockSkins.indexOf(skinId) === -1;
  },
  
  /**
   * 取当前正在使用的carId
   * @returns {number}
   */
  getUsingSkinId() {
    if (this.gameData.usingSkinId === 'PurpleBeetle_1') {  //容错, PurpleBeetle_1虫仔被删了
      this.gameData.usingSkinId = "Default";
    }
    return this.gameData.usingSkinId;
  },
  
  /**
   * 设置当前使用的skinId
   * @param skinId
   */
  setUsingSkinId(skinId) {
    this.gameData.usingSkinId = skinId;
    cc.CEventManager.instance.dispatchEvent(EVENTS.USING_SKIN_CHANGE, skinId);
    this.saveData();
  },
  
  /**
   * 解锁Car
   * @param skinId
   */
  unlockSkin(skinId) {
    if (this.isSkinLocked(skinId)) {
      this.gameData.unlockSkins.push(skinId);
      cc.CEventManager.instance.dispatchEvent(EVENTS.SKIN_UNLOCKED, skinId);
      this.saveData();
      LocalData.setItem('hasNewSkin', true);
    }
  },
  /**
   * 返回已解锁skin id数组
   * @returns {number[]}
   */
  getUnlockedSkinIds() {
    return this.gameData.unlockSkins;
  },
  
  
  /**
   * 取已签到天数
   * @returns {number}
   */
  getSigninDays() {
    return this.gameData.signinDays;
  },
  
  /**
   * 已签到天数加1
   */
  incSigninDays() {
    this.gameData.signinDays++;
    this.saveData();
    cc.CEventManager.instance.dispatchEvent(EVENTS.SIGNINED);
    
  },
  
  getPassTimes(gameMode) {
    if (this.gameData.passTimes === undefined) {
      this.gameData.passTimes = {
        chicken: 0,
        time: 0,
        endless: 0,
      };
    }
    switch (gameMode) {
      case GameModeType.CHICKEN:
        return this.gameData.passTimes.chicken === undefined ? 0 : this.gameData.passTimes.chicken;
      case GameModeType.TIME:
        return this.gameData.passTimes.time === undefined ? 0 : this.gameData.passTimes.time;
      case GameModeType.ENDLESS:
        return this.gameData.passTimes.endless === undefined ? 0 : this.gameData.passTimes.endless;
    }
  },
  
  incPassTimes(gameMode) {
    if (this.gameData.passTimes === undefined) {
      this.gameData.passTimes = {
        chicken: 0,
        time: 0,
        endless: 0,
      };
    }
    switch (gameMode) {
      case GameModeType.CHICKEN:
        this.gameData.passTimes.chicken++;
        break;
      case GameModeType.TIME:
        this.gameData.passTimes.time++;
        break;
      case GameModeType.ENDLESS:
        this.gameData.passTimes.endless++;
        break;
    }
    this.saveData();
  },
  
  
  getTicket() {
    if (this.gameData.ticket === undefined) {
      this.gameData.ticket = 0;
    }
    return this.gameData.ticket;
  },
  
  addTicket(addTicket, worldPos) {
    if (this.gameData.ticket === undefined) {
      this.gameData.ticket = 0;
    }
    this.gameData.ticket += addTicket;
    this.saveData();
    cc.CEventManager.instance.dispatchEvent(EVENTS.PLAYER_TICKET_CHANGE, {
      ticket: this.gameData.ticket,
      worldPos
    });
  },
  
  setTicket(ticket, worldPos) {
    this.gameData.ticket = ticket;
    this.saveData();
    cc.CEventManager.instance.dispatchEvent(EVENTS.PLAYER_TICKET_CHANGE, {
      ticket: this.gameData.ticket,
      worldPos
    });
  },
  
  
});